package com.orcs.leefei.assistant.presenter.component;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Path;
import android.support.annotation.Nullable;
import android.util.AttributeSet;

import com.orcs.leefei.assistant.R;

/**
 * Created by Administrator on 2017/4/10.
 */

public class WeatherScatteredCloud extends WeatherView {
    private static final float CLOUD_DRIFT_STEP = 0.02F;
    private static final float MAX_CLOUD_DRIFT_RATIO = 0.4F;
    private static final float MIN_CLOUD_DRIFT_RATIO = 0.1F;

    private float cloudDriftRatio;
    private boolean driftRight = true;
    private Path cloudPath;

    public WeatherScatteredCloud(Context context) {
        this(context, null);
    }

    public WeatherScatteredCloud(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void init(Context context) {
        super.init(context);
        cloudPath = createPath(R.string.weather_day_cloud);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        scaleToViewBounds(cloudPath);
        cloudDriftRatio = 0.2F;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.save();
        canvas.translate(viewWidth * cloudDriftRatio, viewHeight * 0.25F);
        canvas.drawPath(cloudPath, paint);
        canvas.restore();

        updateCloud();

        postInvalidateDelayed(150);
    }

    private void updateCloud() {
        if (driftRight) {
            cloudDriftRatio += CLOUD_DRIFT_STEP;
            if (cloudDriftRatio >= MAX_CLOUD_DRIFT_RATIO) {
                driftRight = false;
            }
        } else {
            cloudDriftRatio -= CLOUD_DRIFT_STEP;
            if (cloudDriftRatio <= MIN_CLOUD_DRIFT_RATIO) {
                driftRight = true;
            }
        }
    }

}
